﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using ConstructionBidOnline.Biz;
using ConstructionBidOnline.Common;

public partial class admin_ManageNewsRelease : System.Web.UI.Page
{
    #region Types

    public enum LayoutType
    {
        Add,
        Edit,
        Search,
        View
    }

    #endregion

    #region Constants

    private const string PROP_DATASRC_NEWSRELEASE = "_NewsRelease::NR";
    private const string PROP_LAYOUT = "_NewsRelease::UIL";
    private const string PROP_SELECTED_NEWSRELEASE_ID = "_NewsRelease::SNRID";

    #endregion

    #region Public Properties

    public NewsReleaseCollection DataSourceNewsRelease
    {
        get { return (SessionMgr.Session[PROP_DATASRC_NEWSRELEASE] is NewsReleaseCollection ? (NewsReleaseCollection)SessionMgr.Session[PROP_DATASRC_NEWSRELEASE] : (NewsReleaseCollection)(SessionMgr.Session[PROP_DATASRC_NEWSRELEASE] = new NewsReleaseCollection())); }
        set { SessionMgr.Session[PROP_DATASRC_NEWSRELEASE] = value; }
    }

    public int SelectedNewsReleaseID
    {
        get { return (SessionMgr.Session[PROP_SELECTED_NEWSRELEASE_ID] is int ? (int)SessionMgr.Session[PROP_SELECTED_NEWSRELEASE_ID] : 0); }
        set { SessionMgr.Session[PROP_SELECTED_NEWSRELEASE_ID] = value; }
    }


    public LayoutType UILayout
    {
        get { return (SessionMgr.Session[PROP_LAYOUT] is LayoutType ? (LayoutType)SessionMgr.Session[PROP_LAYOUT] : LayoutType.Search); }
        set { SessionMgr.Session[PROP_LAYOUT] = value; }
    }


    #endregion

    #region Event Handlers

    #region Page Load

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);

        if (!Page.IsPostBack)
        {
            DataSourceNewsRelease = NewsRelease.GetAllNewsRelease();
            NewsReleaseList1.DataBind(DataSourceNewsRelease);
        }
    }

    #endregion

    #region Control: NewsReleaseList

    protected void ucNewsReleaseList_EditRequested(object sender, Controls_NewsReleaseList.EditRequestedEventArgs evt)
    {
        SelectedNewsReleaseID = evt.NR.NewsReleaseID;
        NewsReleaseViewEdit1.DataBind(evt.NR);
        SetLayout(LayoutType.Edit);
        divexistname.Visible = false;
    }


    protected void ucNewsReleaseList_AddNewRequested(object sender, EventArgs evt)
    {
        NewsReleaseViewEdit1.CancelEdit();
        NewsRelease newNewsRelease = NewsRelease.CreateNew(DataSourceNewsRelease);
        SelectedNewsReleaseID = newNewsRelease.NewsReleaseID;
        NewsReleaseViewEdit1.DataBind(DataSourceNewsRelease.FindByNewsReleaseID(SelectedNewsReleaseID));
        SetLayout(LayoutType.Edit);
    }


    protected void ucNewsReleaseList_DataSourceRequested(object sender, EventArgs evt)
    {
        DataSourceNewsRelease = NewsRelease.GetAllNewsRelease();
        NewsReleaseList1.DataSource = DataSourceNewsRelease;
    }

    #endregion

    #region Control: NewsReleaseViewEdit

    protected void ucNewsReleaseViewEdit_DataSourceUserRequested(object sender, EventArgs evt)
    {
        NewsReleaseViewEdit1.DataSourceNewsRelease = DataSourceNewsRelease.FindByNewsReleaseID(SelectedNewsReleaseID);
    }

    #endregion

    #region Save / Cancel Buttons

    protected void btnCancel_Click(object sender, ImageClickEventArgs e)
    {
        NewsReleaseViewEdit1.CancelEdit();
        // If we were adding a new NewsRelease...then remove the incomplete/not added new NewsRelease object from the collection.
        if (DataSourceNewsRelease.FindByNewsReleaseID(SelectedNewsReleaseID).ObjState == BOBase.ObjectState.Added)
        {
            try
            {
                DataSourceNewsRelease.Remove(DataSourceNewsRelease.FindByNewsReleaseID(SelectedNewsReleaseID));
                SelectedNewsReleaseID = (DataSourceNewsRelease[0] != null ? DataSourceNewsRelease[0].NewsReleaseID : 0);
            }
            catch
            {
            }
        }
        NewsReleaseList1.DataBind(DataSourceNewsRelease);
        SetLayout(LayoutType.View);
    }
    protected void btnsave_Click(object sender, ImageClickEventArgs e)
    {

        if (NewsReleaseViewEdit1.EndEdit())
        {

            NewsRelease obj = DataSourceNewsRelease.FindByNewsReleaseID(SelectedNewsReleaseID);
            NewsRelease.Update(obj);

            // TODO: Set Feedback text...

            NewsReleaseList1.DataBind(DataSourceNewsRelease);
            SetLayout(LayoutType.View);
            divexistname.Visible = false;
        }
        else
        {
            divexistname.Visible = true;
        }
    }


    #endregion

    #endregion

    #region Private

    #region SetLayout

    /// <summary>
    /// Configures the UI for the specified 'uiLayoutType'.
    /// </summary>
    protected void SetLayout(LayoutType uiLayoutType)
    {
        switch (uiLayoutType)
        {
            case LayoutType.Add:
            case LayoutType.Edit:

                pnlNewsReleaseList.Visible = false;
                pnlNewsReleaseViewEdit.Visible = true;
                pnlButtons.Visible = true;
                break;

            case LayoutType.View:
            case LayoutType.Search:

                pnlNewsReleaseList.Visible = true;
                pnlNewsReleaseViewEdit.Visible = false;
                pnlButtons.Visible = false;
                break;

            default:
                throw new NotImplementedException("The specified LayoutType is not supported in this release.");
        }

        //SetInitialFocus(uiLayoutType);
    }

    #endregion

    #endregion
}
